package sourceapi.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.*;

public class MenuItem extends VerticalPanel implements ClickHandler, Comparable<MenuItem> {
	
	public int Id;
	public String Name;
	public FlowPanel MenuFramework;

	public MenuItem( int id, JSONObject IncludeFile ){

		this.Id = id;
		this.Name = IncludeFile.get("name").isString().stringValue();
		
		this.MenuFramework = new FlowPanel();
		this.MenuFramework.setStyleName("MenuSubItems"); //Make sure the font is on the right size		
		this.MenuFramework.setVisible( false ); //Starts invisible
		
		Anchor menuclick = new Anchor( "<img src='images/file.gif'> " + this.Name, true );
		menuclick.addClickHandler( this );
		
		ArrayList<MenuFunction> Functions = new ArrayList<MenuFunction>();
		JSONObject FuncList = IncludeFile.get("funclist").isObject();
		
		if( FuncList != null ) {
			
			Iterator<String> FuncListIter = FuncList.keySet().iterator();
			
			while( FuncListIter.hasNext() ){
				
				String Key = FuncListIter.next();
				int KeyId = Integer.valueOf( Key );
				JSONArray DoubleArray = FuncList.get( Key ).isArray();
				
				MenuFunction Item = new MenuFunction( KeyId, DoubleArray );
				
				Functions.add( Item );
				
			}
		
		}
		
		//We first need the items sorted by name before adding them
		Collections.sort( Functions );
		
		this.add( menuclick );
		this.add( this.MenuFramework );
		
		for( Widget Item : Functions ){
			this.MenuFramework.add( Item );			
		}
		
	}
	
	public void OpenMenu(){
		
		for( Widget Item : this.MenuFramework ){
			Item.setVisible( true );
		}
		
		this.setVisible( true );
		this.MenuFramework.setVisible( true );

	}
	
	public void OpenSearchMenu(){
		
		String SearchPatern = SourceAPI.MainSearchBox.getText().toLowerCase();
		
		for( Widget Item : this.MenuFramework ){
			Item.setVisible( ((MenuFunction) Item).SearchResult( SearchPatern ) );
		}
		
		if( this.GetVisibledCount() > 0 ){
			this.setVisible( true );
			this.MenuFramework.setVisible( true );			
		} else {
			this.setVisible( false );
		}
		
	}
	
	/**
	 * Returns the number of child functions being presented
	 * If there is none, 0 is returned
	 */
	public int GetVisibledCount(){
		int Count = 0;
		for( Widget Item : this.MenuFramework ){
			if( Item.isVisible() )
				Count++;
		}
		return Count;		
	}
	
	/**
	 * Hides all the functions
	 */
	public void CloseMenu(){
		
		this.MenuFramework.setVisible( false );
		this.setVisible( true );
		
	}

	public void onClick(ClickEvent event) {
		
		if( this.MenuFramework.isVisible() ){
			this.CloseMenu();
			return;			
		}
		
		MenuManager.CloseAllMenus();
		this.OpenMenu();
		
	}

	public int compareTo(MenuItem o) {
		return this.Name.compareTo( o.Name );
	}
	
}
